/*
		LA CONDITION POUR CHANGER IMAGE PION SERA A CHANGER DES QUE LA STRUCTURE SERA DEFINIE

*/
#include "callbacks.h"


void disp_gtk_init(GtkWidget* fixed, Case grille[6][6], GtkWidget* table) {
    int i,j;
    char chaineColonne[] = "A";
    char chaineLigne[] = "1";
    GtkWidget *Colonne[6], *Ligne[6];

    /* initialisation a NULL */
    for ( i = 0 ; i < 6 ; i++ ) {
	for ( j = 0 ; j < 6 ; j++ ) {
		grille[i][j].image = NULL;
		grille[i][j].eventBox = NULL;
	}
    }

    /* on crée la table */
    table = gtk_table_new(7, 7, TRUE);
    
     /* on créé le marquage des lignes et colonnes*/
	  for ( i = 0 ; i < 6 ; i++ ) {
		  Colonne[i] = gtk_label_new(chaineColonne);
		  Ligne[i] = gtk_label_new(chaineLigne);
		  chaineColonne[0]++;
		  chaineLigne[0]++;
		  /* je met le widget dans la table. il faut donne le coin superieur gauche et inferieur droit.*/
		  gtk_table_attach_defaults(GTK_TABLE(table), Colonne[i], i+1, i+2, 0, 1); /* x gauche, x droit, yhaut, ybas*/
		  gtk_table_attach_defaults(GTK_TABLE(table), Ligne[i], 0, 1, i+1, i+2);
	  }

    /* ajout des eventBox pour rendre les cases cliquables */
    for ( i = 0 ; i < 6 ; i++ ) {
	    for ( j = 0 ; j < 6 ; j++ ) {
		    grille[i][j].coord[0] = (gint)i;
		    grille[i][j].coord[1] = (gint)j;
		    fprintf(stderr, "%d %d\n", grille[i][j].coord[0], grille[i][j].coord[1]);
		    
		    grille[i][j].eventBox = gtk_event_box_new();
		    /* on rend la zone cliquable invisible*/
		   gtk_event_box_set_visible_window(GTK_EVENT_BOX(grille[i][j].eventBox), false);

		    /* on l'attache */
		    gtk_table_attach_defaults (GTK_TABLE(table), grille[i][j].eventBox, j+1, j+2, i+1, i+2);

		    
		    
		    /* On ajoute le signal "button_press_event" au GtkEventBox.*/
		    g_signal_connect(G_OBJECT(grille[i][j].eventBox), "button_press_event", G_CALLBACK(disp_gtk_on_press_square), grille[i][j].coord);
	    }
    }
    
    /* on ajoute les pions aux eventBox */
    for ( i = 0 ; i < 2 ; i++ ) {
	    for ( j = 0 ; j < 6 ; j++ ) {
		    /* affichage pions joueur */
		    grille[5-i][j].image = gtk_image_new_from_file(pawnPlayer);
		    gtk_widget_show(grille[5-i][j].image);
		    gtk_container_add (GTK_CONTAINER (grille[5-i][j].eventBox), grille[5-i][j].image);
		     
		     /* affichage pions ordi */
		     grille[i][j].image = gtk_image_new_from_file(pawnComputer);
		     gtk_widget_show(grille[i][j].image);
		     gtk_container_add (GTK_CONTAINER (grille[i][j].eventBox), grille[i][j].image);
	    }
    }
    
    /* on met la table dans le fix */
    gtk_fixed_put(GTK_FIXED(fixed), table, 55, 55);

}

int main(int argc, char *argv[]) {
  /*gestion affichage */
  GtkWidget *mainWindows; /* Fenetre principale */
  GtkWidget *VBox;
  GtkWidget *fixed;
  GtkWidget *imageFond;
  GtkWidget *imageGrille;
  GtkWidget *Table;


  /* affichage du menu */
  GtkWidget* MenuBar; /* barre de menu */
  GtkWidget* Menu;
  GtkWidget* subMenu;
  GtkWidget* MenuItem;
  GSList *pList; /* liste pour le choix du niveau et du theme */

  Case grille[6][6];

  level = LEVEL_EASY;
  pawnPlayer = "pion2.png";
  pawnComputer = "pion3.png";
  background = "fond1.jpg";


  /* initialisation de gtk */
  gtk_init(&argc, &argv);


  /*creation de la fenetre */
  mainWindows = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  /* nom de la fenetre */
  gtk_window_set_title(GTK_WINDOW(mainWindows), "Surakarta v0.1");
  /* taille de la fenetre */
  gtk_window_set_default_size(GTK_WINDOW(mainWindows), X_WINDOWS, Y_WINDOWS);
  /* on rend la fenetre NON-redimenionnable */
  gtk_window_set_resizable(GTK_WINDOW(mainWindows), FALSE);


  /* on cree une boite vertivale permettant d'empiler les widgets les uns sous les autres */
  VBox = gtk_vbox_new(FALSE, 0); /* FALSE = les widgets ne font pas la meme taille */

  /* creation du fixed qui contient le fond */
  fixed = gtk_fixed_new();
 
  /* Chargement de l'image de fond a partir d'un fichier */
  imageFond = gtk_image_new_from_file(background);
  gtk_fixed_put(GTK_FIXED(fixed), imageFond, 0, 0);

  imageGrille = gtk_image_new_from_file("grille.png");
  gtk_fixed_put(GTK_FIXED(fixed), imageGrille, 100, 100); /* remplacer 100 par X_WINDOWS - size(imageGrille) / 2 ?? */
    


/*----------------------------------debut creation du menu-----------------------------------------------------------*/
  /* Creation du menu*/
  MenuBar = gtk_menu_bar_new(); /* barre de menu */
	
    /* menu "fichier" */

	/* nouvelle partie */
	Menu = gtk_menu_new();
	MenuItem = gtk_menu_item_new_with_label("Nouvelle partie");
	gtk_menu_shell_append(GTK_MENU_SHELL(Menu), MenuItem); 

	/* nouvelle partie */
	MenuItem = gtk_menu_item_new_with_label("Sauvegarder");
	gtk_menu_shell_append(GTK_MENU_SHELL(Menu), MenuItem); 

	/* sauvegarder */
	MenuItem = gtk_menu_item_new_with_label("Charger une partie");
	gtk_menu_shell_append(GTK_MENU_SHELL(Menu), MenuItem);
 
	/* barre separatrice */
	MenuItem = gtk_separator_menu_item_new();
	gtk_menu_shell_append(GTK_MENU_SHELL(Menu), MenuItem);
	  
	/* quitter*/
	MenuItem = gtk_menu_item_new_with_label("Quitter");
	g_signal_connect(G_OBJECT(MenuItem), "activate", G_CALLBACK(disp_gtk_on_quit), (GtkWidget*) mainWindows);
	gtk_menu_shell_append(GTK_MENU_SHELL(Menu), MenuItem);
	  
    /* on met tout ce qui precede dans Fichier */
    MenuItem = gtk_menu_item_new_with_label("Fichier");
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(MenuItem), Menu);
    gtk_menu_shell_append(GTK_MENU_SHELL(MenuBar), MenuItem);
	  


    /* menu "Parametres" */  
    Menu = gtk_menu_new();

	/* sous-menu "Niveau" */
	
	      subMenu = gtk_menu_new();

	      /* sous menu choix du niveau */
	      MenuItem = gtk_radio_menu_item_new_with_label(NULL, "facile");
	      gtk_menu_shell_append(GTK_MENU_SHELL(subMenu), MenuItem);
	      pList = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(MenuItem));
	      g_signal_connect(G_OBJECT(MenuItem), "activate", G_CALLBACK(disp_gtk_on_change_level), NULL);

	      MenuItem = gtk_radio_menu_item_new_with_label(pList, "moyen");
	      gtk_menu_shell_append(GTK_MENU_SHELL(subMenu), MenuItem);
	      pList = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(MenuItem));
	      g_signal_connect(G_OBJECT(MenuItem) ,"activate", G_CALLBACK(disp_gtk_on_change_level), NULL);

	      MenuItem = gtk_radio_menu_item_new_with_label(pList, "difficile");
	      gtk_menu_shell_append(GTK_MENU_SHELL(subMenu), MenuItem);
	      pList = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(MenuItem));
	      g_signal_connect(G_OBJECT(MenuItem) ,"activate", G_CALLBACK(disp_gtk_on_change_level), NULL);


	  /* on met tout ce qui precede dans level */
	  MenuItem = gtk_menu_item_new_with_label("niveau de jeu");
	  gtk_menu_item_set_submenu(GTK_MENU_ITEM(MenuItem), subMenu);
	  gtk_menu_shell_append(GTK_MENU_SHELL(Menu), MenuItem);
	  gtk_menu_item_set_submenu(GTK_MENU_ITEM(MenuItem), subMenu);

	/* sous menu choix du background */

	      subMenu = gtk_menu_new();

	      MenuItem = gtk_radio_menu_item_new_with_label(NULL, "desert");
	      gtk_menu_shell_append(GTK_MENU_SHELL(subMenu), MenuItem);
	      pList = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(MenuItem));
	      g_signal_connect(G_OBJECT(MenuItem), "activate", G_CALLBACK(disp_gtk_on_change_background), imageFond);

	      MenuItem = gtk_radio_menu_item_new_with_label(pList, "elephant");
	      gtk_menu_shell_append(GTK_MENU_SHELL(subMenu), MenuItem);
	      pList = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(MenuItem));
	      g_signal_connect(G_OBJECT(MenuItem) ,"activate", G_CALLBACK(disp_gtk_on_change_background), imageFond);



	  /* on met tout ce qui precede dans image de fond */
         MenuItem = gtk_menu_item_new_with_label("image de fond");
	 gtk_menu_item_set_submenu(GTK_MENU_ITEM(MenuItem), subMenu);
	 gtk_menu_shell_append(GTK_MENU_SHELL(Menu), MenuItem);
	 gtk_menu_item_set_submenu(GTK_MENU_ITEM(MenuItem), subMenu);


	/* sous menu choix du background */

	      subMenu = gtk_menu_new();

	      MenuItem = gtk_radio_menu_item_new_with_label(NULL, "wood");
	      gtk_menu_shell_append(GTK_MENU_SHELL(subMenu), MenuItem);
	      pList = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(MenuItem));
	      g_signal_connect(G_OBJECT(MenuItem), "activate", G_CALLBACK(disp_gtk_on_change_pawns), (gpointer)grille);

	      MenuItem = gtk_radio_menu_item_new_with_label(pList, "pion2");
	      gtk_menu_shell_append(GTK_MENU_SHELL(subMenu), MenuItem);
	      pList = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(MenuItem));
	      g_signal_connect(G_OBJECT(MenuItem) ,"activate", G_CALLBACK(disp_gtk_on_change_pawns), (gpointer)grille);



	  /* on met tout ce qui precede dans image de fond */
         MenuItem = gtk_menu_item_new_with_label("pion");
	 gtk_menu_item_set_submenu(GTK_MENU_ITEM(MenuItem), subMenu);
	 gtk_menu_shell_append(GTK_MENU_SHELL(Menu), MenuItem);
	 gtk_menu_item_set_submenu(GTK_MENU_ITEM(MenuItem), subMenu);


	/* on met les deux sous menus dans Parametres*/
	MenuItem = gtk_menu_item_new_with_label("Parametres");
	gtk_menu_item_set_submenu(GTK_MENU_ITEM(MenuItem), Menu);
	gtk_menu_shell_append(GTK_MENU_SHELL(MenuBar), MenuItem);


    /* menu "Aide" */

	  /* A propos */
	  Menu = gtk_menu_new();
	  MenuItem = gtk_menu_item_new_with_label("A propos...");
	  g_signal_connect(G_OBJECT(MenuItem), "activate", G_CALLBACK(disp_gtk_on_about), (GtkWidget*) mainWindows);
	  gtk_menu_shell_append(GTK_MENU_SHELL(Menu), MenuItem);

	  /* Rules */
	  MenuItem = gtk_menu_item_new_with_label("Rules");
	  g_signal_connect(G_OBJECT(MenuItem), "activate", G_CALLBACK(disp_gtk_on_rules), (GtkWidget*) mainWindows);
	  gtk_menu_shell_append(GTK_MENU_SHELL(Menu), MenuItem);
	  
	  /* Aide */
	  /* Faire une fenetre avec les règles */
	  MenuItem = gtk_menu_item_new_with_label("Aide");
	  gtk_menu_item_set_submenu(GTK_MENU_ITEM(MenuItem), Menu);
	  gtk_menu_shell_append(GTK_MENU_SHELL(MenuBar), MenuItem);

/*-----------------------------------fin creation du menu------------------------------------------------------*/

  

  /* creation de la table */  
  disp_gtk_init(fixed, grille, Table);
  /* Ajout du menu a la VBox */
  gtk_box_pack_start(GTK_BOX(VBox), MenuBar, FALSE, FALSE, 0); /* pourquoi false ? */
  /* Ajout du fixed a la VBox */
  gtk_box_pack_start(GTK_BOX(VBox), fixed, FALSE, FALSE, 0);


  /* connexion des signaux */
  /* on ferme l'application ( disp_gtk_on_quit ) si mainWindows recoit un signal "destroy" ( croix en haut a droite  */
  g_signal_connect(G_OBJECT(mainWindows), "destroy", G_CALLBACK(disp_gtk_on_quit),
(gpointer)mainWindows);

  /* Ajout de la VBox a le fenetre principale */
  gtk_container_add(GTK_CONTAINER(mainWindows), VBox);

  /* on affiche tous les widgets de la fenetre*/
  gtk_widget_show_all(mainWindows); 

  /* boucle principale de gtk*/
  gtk_main(); 
 
  return(0);
 }
